package com.spark.check

import android.content.Context
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.shizhefei.view.indicator.IndicatorViewPager
import com.shizhefei.view.indicator.slidebar.ColorBar
import com.spark.common.BaseFragment
import com.spark.common.ext.dp2px
import com.spark.common.helper.TabBarTextListener
import com.spark.common.widget.BadgeTextView
import kotlinx.android.synthetic.main.frag_whole_tasks.*

/**
 * 全部任务
 */
class WholeTasksFragment : BaseFragment() {
    companion object {
        fun newInstance(): WholeTasksFragment {
            val fragment = WholeTasksFragment()
            val args = Bundle()

            fragment.arguments = args
            return fragment
        }
    }

    override fun contentResId(): Int {
        return R.layout.frag_whole_tasks
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        tv_title.text = "全部任务"
        iv_back.setOnClickListener { onBackPressedSupport() }

        activity?.apply {
            pager.isCanScroll = true

            tab.onTransitionListener = TabBarTextListener().setColorId(this, R.color.tab_selected_color, R.color.tab_normal_color)
            val vp = IndicatorViewPager(tab, pager)

            val colorBar = ColorBar(activity, resources.getColor(R.color.tab_selected_color), 3)
            colorBar.setWidth(dp2px(20F).toInt())
            vp.setIndicatorScrollBar(colorBar)
        }


    }

    inner class WholeTasksAdapter(var context: Context, fm: FragmentManager, var items: Array<String>) : IndicatorViewPager.IndicatorFragmentPagerAdapter(fm) {
        override fun getViewForTab(position: Int, convertView: View?, container: ViewGroup?): View? {
            var converter = convertView
            if (converter == null) {
                converter = LayoutInflater.from(context).inflate(R.layout.tab_item, container, false)
            }
            converter?.apply {
                findViewById<BadgeTextView>(R.id.tab_text).apply {
                    text = items[position]
                    textSize = context.dp2px(14F)
                }
            }
            return converter
        }

        override fun getFragmentForPage(position: Int): Fragment {
            return WholeTasksItemFragment.newInstance(position)
        }

        override fun getCount(): Int {
            return items.size
        }

    }
}